package com.example.demo.utils;

import java.io.File;
import java.io.FileOutputStream;

/**
 * @author vsalw
 */
public class FileUtil {
    /**
     * 输入文件字节数据，路径，文件名 写文件
     *
     * @param file
     * @param filePath
     * @param fileName
     * @throws Exception
     */
    public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
        File targetFile = new File(filePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath + fileName);
        out.write(file);
        out.flush();
        out.close();
        System.out.println("file:" + fileName + " write  Success");
    }

    /**
     * 判断文件是否存在
     *
     * @param path 文件地址
     * @return
     */
    public static boolean checkfile(String path) {
        File file = new File(path);
        if (!file.isFile()) {
            return false;
        }
        return true;
    }

    /**
     * 输入文件名，返回文件ContextType即文件类型
     *
     * @param fileName
     * @return
     */
    public static String getContxet(String fileName) {
        String houzi = fileName.substring(fileName.lastIndexOf("."), fileName.length());
        houzi = houzi.toUpperCase();
        String con = "text/html";
        switch (houzi) {
            case ".BMP":
                con = "image/bmp";
                break;
            case ".GIF":
                con = "image/gif";
                break;
            case ".JPG":
                con = "image/jpg";
                break;
            case ".JPE":
                con = "image/jpeg";
                break;
            case ".PNG":
                con = "image/png";
                break;
            default:
        }
        return con;
    }

    /**
     * 常用系统属性和目录
     */
    public static void showSysteminfo() {

        System.out.println("java_vendor:" + System.getProperty("java.vendor"));
        System.out.println("java_vendor_url:"
                + System.getProperty("java.vendor.url"));
        System.out.println("java_home:" + System.getProperty("java.home"));
        System.out.println("java_class_version:"
                + System.getProperty("java.class.version"));
        System.out.println("java_class_path:"
                + System.getProperty("java.class.path"));
        System.out.println("os_name:" + System.getProperty("os.name"));
        System.out.println("os_arch:" + System.getProperty("os.arch"));
        System.out.println("os_version:" + System.getProperty("os.version"));
        System.out.println("user_name:" + System.getProperty("user.name"));
        System.out.println("user_home:" + System.getProperty("user.home"));
        System.out.println("user_dir:" + System.getProperty("user.dir"));
        System.out.println("java_vm_specification_version:"
                + System.getProperty("java.vm.specification.version"));
        System.out.println("java_vm_specification_vendor:"
                + System.getProperty("java.vm.specification.vendor"));
        System.out.println("java_vm_specification_name:"
                + System.getProperty("java.vm.specification.name"));
        System.out.println("java_vm_version:"
                + System.getProperty("java.vm.version"));
        System.out.println("java_vm_vendor:"
                + System.getProperty("java.vm.vendor"));
        System.out
                .println("java_vm_name:" + System.getProperty("java.vm.name"));
        System.out.println("java_ext_dirs:"
                + System.getProperty("java.ext.dirs"));
        System.out.println("file_separator:"
                + System.getProperty("file.separator"));
        System.out.println("path_separator:"
                + System.getProperty("path.separator"));
        System.out.println("line_separator:"
                + System.getProperty("line.separator"));

    }
}
